package com.xucranger.ojapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xucranger.ojapi.enity.DTO.CodeforcesDTO;
import com.xucranger.ojapi.enity.DTO.CodeforcesQueryDTO;
import com.xucranger.ojapi.enity.DTO.RankDTO;
import com.xucranger.ojapi.enity.DTO.common.PageParamDTO;
import com.xucranger.ojapi.enity.DTO.common.PageResultDTO;
import com.xucranger.ojapi.enity.DTO.common.ResponseDTO;
import com.xucranger.ojapi.enity.VO.CareeVO;
import com.xucranger.ojapi.enity.VO.CodeforcesVO;
import com.xucranger.ojapi.enity.VO.RankVO;
import com.xucranger.ojapi.enity.domain.Atcoder;
import com.xucranger.ojapi.enity.domain.Codeforces;
import com.xucranger.ojapi.service.CodeforcesService;
import com.xucranger.ojapi.mapper.CodeforcesMapper;
import com.xucranger.ojapi.utils.SmartBeanUtil;
import com.xucranger.ojapi.utils.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author xucranger
* @description 针对表【codeforces】的数据库操作Service实现
* @createDate 2022-08-16 10:18:51
*/
@Service
public class CodeforcesServiceImpl extends ServiceImpl<CodeforcesMapper, Codeforces>
implements CodeforcesService {

    @Autowired
    CodeforcesMapper codeforcesMapper;

    @Override
    public ResponseDTO<PageResultDTO<CodeforcesVO>> queryContestById(CodeforcesQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<Codeforces> entities = codeforcesMapper.queryContestByPage(page,queryDTO);
        List<CodeforcesVO> dtoList = SmartBeanUtil.copyList(entities,CodeforcesVO.class);
        page.setRecords(dtoList);
        PageResultDTO<CodeforcesVO> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);

    }

    @Override
    public ResponseDTO<PageResultDTO<CodeforcesVO>> queryStudentByContestname(CodeforcesQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<Codeforces> entities = codeforcesMapper.queryContestByPage(page,queryDTO);
        List<CodeforcesVO> dtoList = SmartBeanUtil.copyList(entities,CodeforcesVO.class);
        page.setRecords(dtoList);
        PageResultDTO<CodeforcesVO> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public ResponseDTO<PageResultDTO<CodeforcesVO>> queryStudentByContestname(String contestname) {
        PageParamDTO pageParam = new PageParamDTO();
        pageParam.setPageSize(10);
        pageParam.setPageNum(1);

        CodeforcesQueryDTO codeforcesQueryDTO = new CodeforcesQueryDTO();
        codeforcesQueryDTO.setCfcontestname(contestname);

        codeforcesQueryDTO.setPageNum(pageParam.getPageNum());
        codeforcesQueryDTO.setPageSize(pageParam.getPageSize());

        return queryStudentByContestname(codeforcesQueryDTO);
    }

    @Override
    public ResponseDTO<CareeVO> queryCareeByCfid(String cfid) {
        List<Codeforces> entities = codeforcesMapper.queryCareeByCfid(cfid);

        List<Integer> scores = new ArrayList<>();
        List<String> contestdates = new ArrayList<>();
        List<String> contestnames = new ArrayList<>();

        for (Codeforces item:entities) {

            contestnames.add(item.getCfcontestname());
            scores.add(Integer.valueOf(item.getCfscore()));
            contestdates.add(item.getCfcontestdate());
        }

        CareeVO resultVO = new CareeVO(scores,contestdates,contestnames);

        return ResponseDTO.succData(resultVO);
    }

    @Override
    public ResponseDTO<PageResultDTO<RankVO>> rank(RankDTO rankDTO) {
        Page page = SmartPageUtil.convert2QueryPage(rankDTO);
        List<RankVO> entities = codeforcesMapper.rank(page,rankDTO.getType());
        page.setRecords(entities);
        PageResultDTO<RankVO> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }
}
